package com.bgrc.hubu12305.common;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 包名:com.bgrc.hubu12305.common
 * 用户:86182
 * 时间:20211211:282021/12/9
 */
@Data
@ApiModel("全局返回类")
public class Result<T> {

    @ApiModelProperty("是否成功")
    private boolean isSuccess;
    @ApiModelProperty("状态码")
    private Integer code;
    @ApiModelProperty("携带消息")
    private String message;
    @ApiModelProperty("返回数据")
    private T data;

    /**
     * 创建返回对象带数据
     * */
    protected static <T> Result<T> build(T data) {
        Result<T> result = new Result<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    /**
     * 创建返回对象带数据和状态码
     * */
    public static <T> Result<T> build(T body, CodeEnum codeEnum) {
        Result<T> result = build(body);
        result.setCode(codeEnum.getCode());
        result.setMessage(codeEnum.getMessage());
        return result;
    }

    /**
     * 创建返回对象自定义状态码和消息
     * */
    public static <T> Result<T> build(Integer code, String message) {
        Result<T> result = build(null);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    /**
     * 创建返回对象设置已有的状态码和消息
     * */
    /**
     *成功带数据
     * */
    public static<T> Result<T> ok(T data){
        Result<T> result = Result.build(data,CodeEnum.SUCCESS);
        result.setSuccess(true);
        return result;
    }

    /**
     * 成功不带数据
     * */
    public static<T> Result<T> ok(){
        Result<T> result = Result.ok(null);
        return result;
    }

    /**
     *  失败带数据
     * */
    public static<T> Result<T> fail(T data){
        Result<T> result = Result.build(data,CodeEnum.FAIL);
        result.setSuccess(false);
        return result;
    }

    /**
     * 失败不带数据
     * */
    public static<T> Result<T> fail(){
        Result<T> result = Result.fail(null);
        return result;
    }

    public Result() {
    }

    public Result(boolean isSuccess, Integer code, String message) {
        this.isSuccess = isSuccess;
        this.code = code;
        this.message = message;
    }

}

